#include "NeoScrollBarPrivate.h"
#include "NeoScrollBar.h"
#include <QAbstractScrollArea>
#include <QPropertyAnimation>

NeoScrollBarPrivate::NeoScrollBarPrivate(QObject *parent)
    : QObject(parent), pExtent(4), pScrollSpeed(1), pWheelSpeed(3),
      pPositionAnimation(new QPropertyAnimation(this))
{
    pPositionAnimation->setDuration(100);
    pPositionAnimation->setEasingCurve(QEasingCurve::Linear);
}

NeoScrollBarPrivate::~NeoScrollBarPrivate() = default;

void NeoScrollBarPrivate::setAnimationValue(int value)
{
    NEO_PRIVATE_POINT(NeoScrollBar)
    if (pPositionAnimation->state() == QAbstractAnimation::Running)
    {
        pPositionAnimation->stop();
    }
    pPositionAnimation->setStartValue(q->value());
    pPositionAnimation->setEndValue(value);
    pPositionAnimation->start();
}

void NeoScrollBarPrivate::updateWidgetGeometry()
{
    NEO_PRIVATE_POINT(NeoScrollBar)
    q->raise();
    QAbstractScrollArea *parent = static_cast<QAbstractScrollArea *>(q->parent());
    if (parent)
    {
        if (q->orientation() == Qt::Horizontal)
        {
            q->setGeometry(0, parent->size().height() - pExtent, parent->size().width(), pExtent);
        }
        else
        {
            q->setGeometry(parent->size().width() - pExtent, 0, pExtent, parent->size().height());
        }
    }
    else
    {
        q->setGeometry(0, 0, pExtent, pExtent);
    }
}